<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 *公告管理
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'edit';
if($_RQ['op']=='edit'){
	$item=$worker;
	unset($item['password']);
	$item['lesson']=pdo_get('jy_kexie_lesson',array('id'=>$item['lesson_id'],'uniacid'=>$_SESSION['uniacid']));
	//课程不存在了
	if(!$item['lesson']['id']){
		$item['lesson_id']=0;
	}
	$item['lesson']=$item['lesson']?:array('id'=>0);
	$return['item']=$item;
	$htmls=array(
		'type'=>'edit',
		'post'=>'post',
		'edit_tab'=>true,
		'edits'=>array(
			array(
				'title'=>'基本信息',
				'list'=>array(
					array('name'=>'headimg','title'=>'头像','type'=>'image'),
					array('name'=>'name','title'=>'姓名','type'=>'string'),
					array('name'=>'username','title'=>'用户名','type'=>'string'),
					array('name'=>'password','title'=>'密码','type'=>'password'),
					array('name'=>'mobile','title'=>'手机号','type'=>'string'),
					array('name'=>'tel','title'=>'固定电话','type'=>'string'),
					array('name'=>'sex','title'=>'性别','type'=>'radio','options'=>'sex'),
					array('name'=>'years','title'=>'出生年月','type'=>'time','time_type'=>'day'),
					array('name'=>'lesson_groupid','title'=>'课程类型','type'=>'select','options'=>'lessongroup'),
					array('name'=>'lesson.title','title'=>'授业课程','type'=>'string'),
					array('name'=>'lesson.on_num','title'=>'课时次数','type'=>'string'),
					array('type'=>'checkbox','title'=>'授课时间','name'=>'on_days','help'=>'','options'=>'on_days'),
					array('type'=>'checkbox','title'=>'授课区域','name'=>'on_areas','help'=>'','options'=>'areas'),
					array('name'=>'on_num','title'=>'上课次数','type'=>'string'),
					array('name'=>'address','title'=>'现居住地','type'=>'string'),
					array('name'=>'show','title'=>'讲课内容','type'=>'text'),
					array('name'=>'desc','title'=>'教师简介','type'=>'richtext'),
					array('name'=>'lessondesc','title'=>'课程简介','type'=>'richtext'),
					array('name'=>'remark','title'=>'备注','type'=>'text'),
					array('type'=>'choose','title'=>'绑定会员','name'=>'mid','action'=>'get_member','choose_title'=>'选择会员'),
					//array('name'=>'status','title'=>'状态','type'=>'radio','options'=>'status'),
				)
			),
		)
	);
	$return['on_days']=array('1'=>'周一','2'=>'周二','3'=>'周三','4'=>'周四','5'=>'周五','6'=>'周六','7'=>'周日');
	$return['sex']=array('1'=>'男','2'=>'女');
	$return['status']=array('0'=>'关闭','1'=>'开启');
	$areas=pdo_getall('jy_kexie_area',array('uniacid'=>$_SESSION['uniacid']));
	foreach($areas as $av){
		$return['areas'][$av['id']]=$av['title'];
	}
	$return['lessongroup']=pdo_getall('jy_kexie_lesson_group',array('uniacid'=>$_SESSION['uniacid']));
	$return['lesson']=pdo_getall('jy_kexie_lesson',array('workerid'=>$this->worker_id,'uniacid'=>$_SESSION['uniacid']));
	$return['htmls']=$htmls;
	set_html('title','讲师后台');
	exi($return);
}
if($_RQ['op']=='post'){
	$postdata=get_postdata();
	if(!$postdata['on_areas']){
		exi('请选择授课区域','error');
	}
	if(!$postdata['lesson_groupid']){
		exi('请选择课程类型','error');
	}
	if($postdata['lesson']['on_num'] && !$postdata['lesson']['title']){
		exi('请设置课程名称！','error');
	}
	//$postdata['time']=ijson_encode($postdata['time']);	
	$data=array_fields(array('address','sex','years','name','username','password','mobile','tel','lesson_id','lessondesc','desc','on_num','on_days','on_areas','headimg','show','remark','paixu','status','lesson_groupid','mid'),$postdata);
	$id=intval($postdata['id']);
	if($data['password']){
		$data['password']=base64_encode($data['password']);
	}
	if($data['years']){
		$data['years']=date('Y-m-d',strtotime($data['years']));
	}
	if($id){
		pdo_update('jy_kexie_worker',$data,array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}else{
		$data['uniacid']=$_SESSION['uniacid'];
		$data['mid']=$_SESSION['uid'];
		$data['createtime']=time();
		pdo_insert('jy_kexie_worker',$data);
	}
	if($postdata['lesson']['title']){
		$lesson=array(
				'title'=>$postdata['lesson']['title'],
				'on_num'=>$postdata['lesson']['on_num'],
				'groupid'=>$data['lesson_groupid'],
		);
		if($data['lesson_id']){
			pdo_update('jy_kexie_lesson',$lesson,array('id'=>$data['lesson_id'],'uniacid'=>$_SESSION['uniacid']));
		}else{
			$lesson['uniacid']=$_SESSION['uniacid'];
			$lesson['workerid']=$this->worker_id;
			pdo_insert('jy_kexie_lesson',$lesson);
			$lesson_id=pdo_insertid();
			pdo_update('jy_kexie_worker',array('lesson_id'=>$lesson_id),array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		}
	}
	if($_SESSION['os']=='windows'){
		exi('操作成功！','','/worker.index/');
	}else{
		exi('操作成功！','','/worker.home/');
	}
}
if($_RQ['op']=='del'){
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
	}else{
		$del=array();
	}
	if($_RQ['id']){
		$del[]=$_RQ['id'];
	}
	foreach($del as $id){
		$id=intval($id);
		if($id){
			pdo_delete('jy_kexie_worker',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		}
	}
	exi('删除成功！');
}
if($_RQ['op']=='set'){
	$id=intval($_RQ['id']);
	if(!$id){
		exi('请先选择要编辑的老师！','error');
	}
	$post=get_postdata();
	if(isset($post['status'])){
		pdo_update('jy_kexie_worker',array('status'=>intval($post['status'])),array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}
	exi('操作成功！');
}